package com.flashcards;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.widget.FrameLayout;
import com.flashcards.vocabulary.Vocabulary;

public class FlashCards extends Activity implements View.OnClickListener {
    private Vocabulary vocabulary;
    private Navigation3D navigation;

    private FrameLayout tranlsationContainer;
    private ViewGroup container;
    private FrameLayout wordContainer;
    private boolean position = false;

    public FlashCards() {
        vocabulary = Vocabulary.createInstance();
        Log.i(">>>>", "vocabulary size - " + vocabulary.size());
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.main);
//
//        ViewFlipper flipper = (ViewFlipper) findViewById(R.id.card);
//        flipper.setInAnimation(this.getApplicationContext(), R.anim.grow_from_middle);
//        flipper.setOutAnimation(this.getApplicationContext(), R.anim.shrink_to_middle);
//        findViewById(R.id.info_btn).setOnClickListener(this);
//        TextView text = (TextView) findViewById(R.id.word);
//        text.setText(vocabulary.first().getEng());
//        findViewById(R.id.done).setOnClickListener(this);
//        flipper.setOnTouchListener(new NavigationByTouchListener(navigation));

        setContentView(R.layout.main);

        tranlsationContainer = (FrameLayout) findViewById(R.id.translation_container);
        wordContainer = (FrameLayout) findViewById(R.id.word_container);
        container = (ViewGroup) findViewById(R.id.container);

        FrameLayout container = (FrameLayout) findViewById(R.id.container);
        navigation = new Navigation3D(container, vocabulary);
        container.setOnTouchListener(new NavigationByTouchListener(navigation));
        findViewById(R.id.info_btn).setOnClickListener(this);
        findViewById(R.id.done).setOnClickListener(this);
        container.setPersistentDrawingCache(ViewGroup.PERSISTENT_ANIMATION_CACHE);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        navigation.onKey(keyCode);
        return super.onKeyDown(keyCode, event);
    }

    public void onClick(View view) {
        position = !position;
        applyRotation(position, 180, 90);
    }

    private void applyRotation(boolean position, float start, float end) {
        // Find the center of the container
        final float centerX = container.getWidth() / 2.0f;
        final float centerY = container.getHeight() / 2.0f;

        // Create a new 3D rotation with the supplied parameter
        // The animation listener is used to trigger the next animation
        final Rotate3dAnimation rotation =
                new Rotate3dAnimation(start, end, centerX, centerY, 310.0f, true);
        rotation.setDuration(500);
        rotation.setFillAfter(true);
        rotation.setInterpolator(new AccelerateInterpolator());
        rotation.setAnimationListener(new DisplayNextView(position));

        container.startAnimation(rotation);
    }

    private final class DisplayNextView implements Animation.AnimationListener {
        private final boolean mPosition;

        private DisplayNextView(boolean position) {
            mPosition = position;
        }

        public void onAnimationStart(Animation animation) {
        }

        public void onAnimationEnd(Animation animation) {
            container.post(new SwapViews(mPosition));
        }

        public void onAnimationRepeat(Animation animation) {
        }
    }

    private final class SwapViews implements Runnable {
        private final boolean mPosition;

        public SwapViews(boolean position) {
            mPosition = position;
        }

        public void run() {
            final float centerX = container.getWidth() / 2.0f;
            final float centerY = container.getHeight() / 2.0f;
            Rotate3dAnimation rotation;

            if (mPosition) {
                tranlsationContainer.setVisibility(View.GONE);
                wordContainer.setVisibility(View.VISIBLE);
                wordContainer.requestFocus();

                rotation = new Rotate3dAnimation(90, 360, centerX, centerY, 310.0f, false);
            } else {
                wordContainer.setVisibility(View.GONE);
                tranlsationContainer.setVisibility(View.VISIBLE);
                tranlsationContainer.requestFocus();

                rotation = new Rotate3dAnimation(90, 0, centerX, centerY, 310.0f, false);
            }

            rotation.setDuration(500);
            rotation.setFillAfter(true);
            rotation.setInterpolator(new DecelerateInterpolator());

            container.startAnimation(rotation);
        }
    }
}